<?php
/**
 * Created by: zhaoyi (834574377@qq.com)
 * Time: 2023/1/9 17:06
 * Blog: https://www.yuque.com/huangzhenlian
 */

declare(strict_types=1);

namespace App\Application\Api\Task;

use App\Annotation\Api;
use App\Exception\ErrorException;
use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;

#[Controller("/api/ApiTask")]
#[Crontab(rule: "*\/5 * * * * *", name: "订单开始", callback: "begin", memo: "订单开始定时更新", enable: "isbegin")]
class ApiTask
{
    //订单状态0未开始，1进行中，2已结束，3已退款，4已取消，5已分账，6待付款
    //订单开始定时更新
    #[Api]
    #[GetMapping("begin")]
    public function begin()
    {
        $where = [];
        $where[] = ['add_time', '<', time()];
        $where[] = ['status', '=', 0];
        $res = Db::table('order')->where($where)->update([
            'status' => 1,
            'updated_at' => date('Y-m-d H:i:s', time())
        ]);
        if ($res) {
            return true;
        } else {
            return false;
        }
    }

    public function isbegin(): bool
    {

        $where = [];
        $where[] = ['add_time', '<', time()];
        $where[] = ['status', '=', 0];
        $order = Db::table('order')->where($where)->select('*')->first();
        if (!empty($order)) {
            return true;
        } else {
            return false;
        }

    }
}